﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Douban.Core.Helpers;
using System.IO;
using System.Net;

namespace Douban
{
    public static class NotifyIconHelper
    {
        private const string FolderName = "IconDownload";

        public static string GetLocalIcon(string userId, string uri)
        { 
            if(!AppFileHelper.DirectoryExists(FolderName)) 
                AppFileHelper.CreateDirectory(FolderName);

            int index = userId.LastIndexOf("/");
            string iconPath = Path.Combine(
                FolderName, userId.Substring(index + 1) + ".jpg");

            if(!AppFileHelper.Exists(iconPath))
            {
                AppFileHelper.Create(iconPath);
                WebClient client = new WebClient();
                byte[] iconData = client.DownloadData(uri);
                AppFileHelper.WriteFile(iconPath,
                    stream => stream.Write(iconData, 0, iconData.Length));
            }

            return AppFileHelper.GetFullPath(iconPath);
        }
    }
}
